﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
    public class ChiTietPhieuMuonSachDAO
    {
#region 1. Them
        public static bool ThemChiTietPMS(ChiTietPhieuMuonSachDTO chitiet ){
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaPM", chitiet.MaPhieuMuon));
                sqlParams.Add(new SqlParameter("@MaTaiLieu", chitiet.MaTaiLieu));

                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_InsertCT_PM", sqlParams);
                if (n == 1)
                    result = true;
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }
#endregion
#region 2. Xoa
        public static bool XoaChiTietPMS(ChiTietPhieuMuonSachDTO chitiet)
        {
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaPM", chitiet.MaPhieuMuon));
                sqlParams.Add(new SqlParameter("@MaTaiLieu", chitiet.MaTaiLieu));

                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_DeleteCT_PM", sqlParams);
                if (n == 1)
                    result = true;
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }
#endregion
#region 4. Tim Kiem
        public static ChiTietPhieuMuonSachDTO TimChiTietPMS(ChiTietPhieuMuonSachDTO chitiet){
            ChiTietPhieuMuonSachDTO result = new ChiTietPhieuMuonSachDTO();
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaPM", chitiet.MaPhieuMuon));
                sqlParams.Add(new SqlParameter("@MaTaiLieu", chitiet.MaTaiLieu));

                DataTable dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectCT_PM", sqlParams);
                DataRow dr = dt.Rows[0];

                result.MaPhieuMuon = dr["MaPM"].ToString();
                result.MaTaiLieu = dr["MaTaiLieu"].ToString();
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return result;
        }
        public static List<ChiTietPhieuMuonSachDTO> TimChiTietPMSByPhieuMuon(String maphieumuon)
        {
            List<ChiTietPhieuMuonSachDTO> result = new List<ChiTietPhieuMuonSachDTO>();
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaPM", maphieumuon));

                DataTable dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectCT_PMsByAndMaPM", sqlParams);
                foreach (DataRow dr in dt.Rows)
                {
                    ChiTietPhieuMuonSachDTO tmp = new ChiTietPhieuMuonSachDTO();

                    tmp.MaPhieuMuon = dr["MaPM"].ToString();
                    tmp.MaTaiLieu = dr["MaTaiLieu"].ToString();

                    result.Add(tmp);
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }
        public static List<ChiTietPhieuMuonSachDTO> TimChiTietPMSByTaiLieu(String matailieu)
        {
            List<ChiTietPhieuMuonSachDTO> result = new List<ChiTietPhieuMuonSachDTO>();
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaTaiLieu", matailieu));

                DataTable dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectCT_PMsByAndMaTaiLieu", sqlParams);
                foreach (DataRow dr in dt.Rows)
                {
                    ChiTietPhieuMuonSachDTO tmp = new ChiTietPhieuMuonSachDTO();

                    tmp.MaPhieuMuon = dr["MaPM"].ToString();
                    tmp.MaTaiLieu = dr["MaTaiLieu"].ToString();

                    result.Add(tmp);
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }
        public static List<ChiTietPhieuMuonSachDTO> TimTatCaChiTietPMS()
        {
            List<ChiTietPhieuMuonSachDTO> result = new List<ChiTietPhieuMuonSachDTO>();
            try
            {
                DataTable dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectCT_PMsAll");
                foreach (DataRow dr in dt.Rows)
                {
                    ChiTietPhieuMuonSachDTO tmp = new ChiTietPhieuMuonSachDTO();

                    tmp.MaPhieuMuon = dr["MaPM"].ToString();
                    tmp.MaTaiLieu = dr["MaTaiLieu"].ToString();

                    result.Add(tmp);
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }
        public static bool CheckPMSByID(ChiTietPhieuMuonSachDTO chitiet){
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaPM", chitiet.MaPhieuMuon));
                sqlParams.Add(new SqlParameter("@MaTaiLieu", chitiet.MaTaiLieu));

                DataTable dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectCT_PM", sqlParams);
                if (dt.Rows.Count > 0)
                {
                    result = true;
                }
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return result;
        }
        public static bool CheckChiTietPhieuMuonSach(String mapm, String matl)
        {
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaPM", mapm));
                sqlParams.Add(new SqlParameter("@MaTaiLieu", matl));

                DataTable dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectCT_PM", sqlParams);
                if (dt.Rows.Count > 0)
                {
                    result = true;
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }
#endregion
    }
}
